<?php

namespace App\Console\Commands;

use App\Models\Website;
use Illuminate\Console\Command;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use function GuzzleHttp\Psr7\try_fopen;

class DictMap extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'DictMap';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->translate( join(DIRECTORY_SEPARATOR,[BASE_PATH(),'resources','lang','zh-CN','table.php']));
        //%3B&oq=%24this->getConnection()->getSchemaBuilder()->getColumnListing(%24this->getTable())%3B&aqs=chrome..69i57&sourceid=chrome&ie=UTF-8
    }


    protected function translate($load)
    {

        $tables = DB::getDoctrineSchemaManager()->listTableNames();
        $map = array_map(function ($table){
            #return $table;
            return  $this->getFieldComment($table);
        }, array_combine($tables, $tables));

        $str = "<?php return ".var_export($map,true). "?>";
        #var_dump($str);
        file_put_contents($load,$str);
    }

    private function getFieldComment($table)
    {
        $tableInstance = DB::getDoctrineSchemaManager()->listTableDetails($table);
        #var_dump($tableInstance->getColumns());exit;
        return array_map(function ($column)
        {
           return $column->getComment()?:$column->getName();
        }, $tableInstance->getColumns());
    }
}
